<?php
$ROOT_DIR_PATH = '../';
require_once($ROOT_DIR_PATH.'includes/includes.php');
require_once($ROOT_DIR_PATH.'classes/class.user.inc.php');
$listingUrl = $ADMIN_BASE_HREF . "changeemail.php";
$loginUrl = $ADMIN_BASE_HREF . "login.php";
$dashBoardUrl = $ADMIN_BASE_HREF . "dashboard.php";

if (!$GENOBJ->isLoggedIn()) {
    $GENOBJ->redirect($loginUrl);
}

if(!$GENOBJ->isAdminLogin()) {
	$GENOBJ->redirect($dashBoardUrl);
}

$ADMIN_PAGE_TITLE = 'Change Email';
$GENOBJ->clearSessionErrorMessages();
$email = $_SESSION[SESSION_NAME]['admin']['email'];
$userObj = new user();
$userId = $_SESSION[SESSION_NAME]['admin']['userid'];
$errorMessageArray = Array();
$succMessageArray = Array();
if ($_POST) {
    $filterElements = Array();
    $filterArray = Array("TrimElement", "htmlEntity");
    $email = $_POST['email'];
    $email = $GENOBJ->filter($email, $filterArray);
    if ($GENOBJ->IsEmpty($email)) {
        $errorMessageArray[] = 'Email address shouldnot be blank.';
    } else if (!$GENOBJ->ValidEmailAddress($email)) {
        $errorMessageArray[] = 'Email address is not valid.';
    }
	
	if (count($errorMessageArray) <= 0) {
		$userExist = $userObj->checkEmailExists($email);
		if ($userExist) {
			$errorMessageArray[] = 'Email-Address already exist';
		}
	}
	
	
    if (count($errorMessageArray) <= 0) {
        $userObj->setemail($email);
        $userObj->setuserid($userId);
        $userObj->changeEmail($email);
        $_SESSION[SESSION_NAME]['admin']['email'] = $email;
        $succMessageArray[] = 'Email Address changed successfully';
    }
}
?>
<?php include('templates/header.php');?>
<body>
        <div id="change-email"></div>
	<div id="ncleangrey-container">
		<?php include('templates/navigation.php');?>
		<div class="breadcrumbs">
			<div id="nav-icons_all">
				<ul id="nav-icons">
					<li class="logout-icon"><a  title="Logout"  href="logout.php">Logout</a></li>
				</ul>
			</div>
               <p class="breadcrumbs">
                    <a title="Home" href="dashboard.php">Home</a> &#187; <a title="content" href="preference.php">My Preferences</a> &#187; Change email
               </p>
		</div>
		<div class="hstippled">&nbsp;</div>
		<div id="MainContent">
			<div>
				<div class="pageerrorcontainer-validator">
					<div class="error-iterator"></div>
				</div>
			</div>
          <?php if(count($succMessageArray) > 0):?>
               <div class="pagemcontainer">
               	<?php foreach($succMessageArray as $succValue):?>
				<p class="pagemessage">
					<img src="images/icons/system/accept.gif" class="systemicon" alt="Success" title="Success" />
					<label class="errorlabel"><?php echo $succValue;?></label>
				</p>
				<?php endforeach;?>
			</div>
			<?php endif;?>
               <div class="clearb">&nbsp;</div>
		<?php if(count($errorMessageArray) > 0):?>
			<div class="pageerrorcontainer">
			<?php foreach($errorMessageArray as $erroValue):?>
				<div class="pageoverflow">
					<img src="images/icons/system/stop.gif" class="systemicon" alt="Error" title="Error" />
					<label class="errorlabel"><?php echo $erroValue;?></label>
				</div>
			<?php endforeach;?>
			</div>
		<?php endif;?>
               <div class="clearb">&nbsp;</div>
               <div id="Edit_Content_Result"></div>
               <div class="pagecontainer pageoverflow">
                    <div class="pageheader">
                      Change email
                      <div class="field-required">* Fields are required </div>
                    </div>
                    <div style="clear: both;"></div>
                    <form method="post" action="changeemail.php" name="changeemailfrm" id="changeemailfrm">
                    	<div id="page_content">
                              <div class="pageoverflow">
                                   <div class="pagetext">Email Address<span class="error-star">*</span>:</div>
                                   <div class="pageinput">
                                        <input type="text" name="email" id="email" value="<?php echo $email;?>" size="60"/>
                                   </div>
                              </div>
                              <div class="pageoverflow">
                                   <p class="pagetext">&nbsp;</p>
                                   <p class="pageinput">
                                        <input type="submit" name="submitbutton" value="Submit"  title="Save changes" />
                                        <input type="reset" name="apply" value="Reset" title="Save changes and continue to edit" />
                                   </p>
                              </div>
                              <div style="clear: both;"></div>
                         </div>
                    </form>
               </div>
               <p class="pageback">
				<a class="pageback" href="dashboard.php">&#171; Back to Home</a>
			</p>
               <div class="clearb"></div>
          </div>
     </div>
     <?php include('templates/footer.php');?>
</body>
</html>